<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>70mm×45mm标签打印测试</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            padding: 30px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }

        .header h1 {
            font-size: 24px;
            margin-bottom: 10px;
        }

        .header p {
            color: #666;
            font-size: 14px;
        }

        /* 标签预览样式 */
        .label-preview {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }

        .label {
            width: 70mm;
            height: 45mm;
            background: white;
            border: 2px solid #333;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            font-weight: bold;
            color: #333;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            position: relative;
        }

        /* 尺寸标注 */
        .label::before {
            content: "70mm";
            position: absolute;
            top: -25px;
            left: 50%;
            transform: translateX(-50%);
            font-size: 12px;
            color: #666;
            font-weight: normal;
        }

        .label::after {
            content: "45mm";
            position: absolute;
            right: -35px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 12px;
            color: #666;
            font-weight: normal;
            writing-mode: vertical-rl;
        }

        /* 控制面板 */
        .controls {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin: 20px 0;
        }

        .control-group {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 15px;
        }

        .control-group:last-child {
            margin-bottom: 0;
        }

        label {
            font-weight: bold;
            color: #333;
            min-width: 80px;
        }

        input[type="text"] {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            flex: 1;
            max-width: 200px;
        }

        input[type="text"]:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s;
            margin-right: 10px;
        }

        .btn-primary {
            background: #007bff;
            color: white;
        }

        .btn-primary:hover {
            background: #0056b3;
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #545b62;
        }

        .btn-success {
            background: #28a745;
            color: white;
        }

        .btn-success:hover {
            background: #1e7e34;
        }

                 /* 打印样式 */
         @media print {
             body {
                 background: white !important;
                 padding: 0 !important;
                 margin: 0 !important;
             }
 
             .container {
                 box-shadow: none !important;
                 padding: 0 !important;
                 max-width: none !important;
                 margin: 0 !important;
             }
 
             .header,
             .controls,
             .label-preview {
                 display: none !important;
             }
 
             /* 显示打印区域 */
             #printArea {
                 display: block !important;
             }
 
             /* 打印时的标签样式 */
             .print-label {
                 display: block !important;
                 width: 35mm !important;
                 height: 35mm !important;
                 margin: 0 !important;
                 padding: 0 !important;
                 border: none !important;
                 box-shadow: none !important;
                 page-break-after: always !important;
                 page-break-inside: avoid !important;
                 font-size: 24px !important;
                 font-weight: bold !important;
                 display: flex !important;
                 align-items: center !important;
                 justify-content: center !important;
                 background: white !important;
                 color: black !important;
             }
 
             .print-label:last-child {
                 page-break-after: auto !important;
             }
         }

        /* 页面设置 */
        @page {
            size: 70mm 45mm;
            margin: 0;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                margin: 10px;
                padding: 20px;
            }

            .label {
                width: 60mm;
                height: 38mm;
                font-size: 20px;
            }

            .control-group {
                flex-direction: column;
                align-items: flex-start;
                gap: 8px;
            }

            input[type="text"] {
                max-width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>70mm×45mm标签打印测试</h1>
            <p>专为标签打印机设计的精确尺寸打印页面</p>
        </div>

        <div class="controls">
            <div class="control-group">
                <label for="labelContent">标签内容:</label>
                <input type="text" id="labelContent" value="123456" placeholder="输入标签内容">
            </div>
            <div class="control-group">
                <label for="labelCount">打印数量:</label>
                <input type="text" id="labelCount" value="1" placeholder="输入打印数量">
            </div>
                         <div class="control-group">
                 <button class="btn btn-primary" onclick="updatePreview()">更新预览</button>
                 <button class="btn btn-success" onclick="printLabels()">打印标签</button>
                 <button class="btn btn-secondary" onclick="downloadPDF()">下载PDF</button>
                 <button class="btn btn-secondary" onclick="showPrintPreview()">显示打印预览</button>
             </div>
        </div>

        <div class="label-preview">
            <div class="label" id="previewLabel">123456</div>
        </div>

        <!-- 打印区域 -->
        <div id="printArea" style="display: none;">
            <!-- 动态生成打印标签 -->
        </div>
    </div>

    <script>
        // 更新预览
        function updatePreview() {
            const content = document.getElementById('labelContent').value || '123456';
            document.getElementById('previewLabel').textContent = content;
        }

                 // 打印标签
         function printLabels() {
             const content = document.getElementById('labelContent').value || '123456';
             const count = parseInt(document.getElementById('labelCount').value) || 1;
             
             // 生成打印内容
             generatePrintContent(content, count);
             
             // 显示打印区域（用于调试）
             document.getElementById('printArea').style.display = 'block';
             
             // 延迟执行打印，确保内容已生成
             setTimeout(() => {
                 window.print();
                 // 打印后隐藏打印区域
                 setTimeout(() => {
                     document.getElementById('printArea').style.display = 'none';
                 }, 1000);
             }, 100);
         }

                 // 生成打印内容
         function generatePrintContent(content, count) {
             const printArea = document.getElementById('printArea');
             printArea.innerHTML = '';
             
             for (let i = 0; i < count; i++) {
                 const label = document.createElement('div');
                 label.className = 'print-label';
                 label.textContent = content;
                 label.style.cssText = `
                     width: 70mm;
                     height: 45mm;
                     margin: 0;
                     padding: 0;
                     border: none;
                     box-shadow: none;
                     font-size: 24px;
                     font-weight: bold;
                     display: flex;
                     align-items: center;
                     justify-content: center;
                     background: white;
                     color: black;
                     page-break-after: ${i < count - 1 ? 'always' : 'auto'};
                     page-break-inside: avoid;
                 `;
                 printArea.appendChild(label);
             }
         }

        // 下载PDF
        async function downloadPDF() {
            // 检查是否加载了jsPDF库
            if (typeof window.jspdf === 'undefined') {
                // 动态加载jsPDF库
                const script = document.createElement('script');
                script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js';
                script.onload = () => {
                    createPDF();
                };
                document.head.appendChild(script);
            } else {
                createPDF();
            }
        }

        // 创建PDF
        async function createPDF() {
            const { jsPDF } = window.jspdf;
            const content = document.getElementById('labelContent').value || '123456';
            const count = parseInt(document.getElementById('labelCount').value) || 1;
            
            try {
                // 创建70mm×45mm的PDF
                const pdf = new jsPDF('p', 'mm', [70, 45]);
                
                for (let i = 0; i < count; i++) {
                    if (i > 0) {
                        pdf.addPage([70, 45]);
                    }
                    
                    // 设置字体
                    pdf.setFontSize(24);
                    pdf.setFont('helvetica', 'bold');
                    
                    // 计算文本位置（居中）
                    const textWidth = pdf.getTextWidth(content);
                    const x = (70 - textWidth) / 2;
                    const y = 45 / 2 + 8; // 垂直居中，+8是为了微调
                    
                    // 添加文本
                    pdf.text(content, x, y);
                }
                
                pdf.save('70x45mm_labels.pdf');
            } catch (error) {
                console.error('PDF生成失败:', error);
                alert('PDF生成失败，请重试');
            }
        }

                 // 显示打印预览
         function showPrintPreview() {
             const content = document.getElementById('labelContent').value || '123456';
             const count = parseInt(document.getElementById('labelCount').value) || 1;
             
             // 生成打印内容
             generatePrintContent(content, count);
             
             // 显示打印区域
             const printArea = document.getElementById('printArea');
             printArea.style.display = 'block';
             printArea.style.border = '2px solid red';
             printArea.style.padding = '20px';
             printArea.style.background = '#f0f0f0';
             
             // 5秒后隐藏
             setTimeout(() => {
                 printArea.style.display = 'none';
                 printArea.style.border = '';
                 printArea.style.padding = '';
                 printArea.style.background = '';
             }, 5000);
         }
         
         // 页面加载完成后初始化
         document.addEventListener('DOMContentLoaded', function() {
             // 监听输入变化，实时更新预览
             document.getElementById('labelContent').addEventListener('input', updatePreview);
             document.getElementById('labelCount').addEventListener('input', function() {
                 const value = this.value;
                 if (value < 1) {
                     this.value = 1;
                 } else if (value > 100) {
                     this.value = 100;
                 }
             });
         });
    </script>
</body>
</html>
